package prickworlds.graphics;

import static org.lwjgl.opengl.GL11.*;

import org.lwjgl.util.Color;

import prickworlds.structure.Drawable;

public class Particle implements Drawable {

	float x;
	float y;
	float width;
	float height;

	float velX;
	float velY;

	float friction;

	float accX;
	float accY;

	ParticleType type;
	boolean finished;
	public int life;

	public Particle(ParticleType type) {
		this.type = type;
		finished = false;
		this.life = type.getFullLife();

		friction = 0.975f;
	}

	@Override
	public void display() {
		Texture tex = type.getTexture();
		Color color = type.getColor(1.0f - (float) life / type.getFullLife());

		glEnable(GL_BLEND);
		glEnable(GL_TEXTURE_2D);
		tex.bind();

		glColor4f(color.getRed() / 255f, color.getGreen() / 255f, color
				.getBlue() / 255f, color.getAlpha() / 255f);

		glBegin(GL_TRIANGLE_STRIP);

		glTexCoord2f(0f, 0f);
		glVertex2f(x, y);

		glTexCoord2f(1f, 0f);
		glVertex2f(x + width, y);

		glTexCoord2f(0f, 1f);
		glVertex2f(x, y + height);

		glTexCoord2f(1f, 1f);
		glVertex2f(x + width, y + height);

		glEnd();

		glDisable(GL_BLEND);
		glDisable(GL_TEXTURE_2D);
	}

	@Override
	public void update() {
		velX += accX;
		velY += accY;

		velX *= friction;
		velY *= friction;

		x += velX;
		y += velY;

		life--;

		if (life <= 0)
			finished = true;
	}

	public boolean isFinished() {
		return finished;
	}
}
